package com.fand.configuration.controller;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fand.common.service.CommonService;
import com.fand.configuration.dto.DictionaryTyDto;
import com.fand.configuration.vo.DictionaryVo;
import com.fand.context.mapper.JsonMapper;
import com.fand.dto.PaginationDto;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

/**
 * 字典
 * 
 * @author yaoHT
 */
@Controller
public class DictionaryController {
	/**
	 * 公共服务
	 */
	@Qualifier(CommonService.BEAN_ID)
	@Autowired(required = false)
	private CommonService commonService;
	
	@RequestMapping(value = { "/admin/configuration/dictionary/reload.json" })
	public @ResponseBody
	Map<String, Object> adminConfigurationDictionaryReload() {
		final Map<String, Object> jsonMap = Maps.newHashMap();
		
		try {
			this.commonService.getConfigurationService().getDictionaryService().reload();
		} catch (final Exception e) {
			jsonMap.put("message", e.getMessage());
		}
		
		return jsonMap;
	}
	
	@RequestMapping(value = { "/admin/configuration/dictionaryTy/delete.json" }, method = RequestMethod.POST)
	public @ResponseBody
	Map<String, Object> adminConfigurationDictionaryTyDelete(final String ids) {
		final Map<String, Object> jsonMap = Maps.newHashMap();
		
		try {
			this.commonService.getConfigurationService().getDictionaryService().deleteDictionaryTyByIds(ids);
		} catch (final Exception e) {
			jsonMap.put("message", e.getMessage());
		}
		
		return jsonMap;
	}
	
	/**
	 * 字典类型主页面
	 * 
	 * @param DictionaryVo
	 *            字典视图对象
	 * @return 模板路径
	 */
	@RequestMapping(value = { "/admin/configuration/dictionaryTy.htm", "/configuration/dictionaryTy/index.htm" }, method = RequestMethod.GET)
	public String adminConfigurationDictionaryTyindex(final DictionaryVo dictionaryVo) {
		return "admin/configuration/dictionaryTy/index";
	}
	
	@RequestMapping(value = { "/admin/configuration/dictionaryTy/save.txt" }, method = RequestMethod.POST)
	public @ResponseBody
	String adminConfigurationDictionaryTySave(final DictionaryVo dictionaryVo) {
		final Map<String, Object> jsonMap = Maps.newHashMap();
		
		try {
			this.commonService.getConfigurationService().getDictionaryService()
			        .saveDictionaryTyBy1ForObject(dictionaryVo.getDictionaryTy());
		} catch (final Exception e) {
			jsonMap.put("message", e.getMessage());
		}
		
		return JsonMapper.nonEmptyMapper().toJson(jsonMap);
	}
	
	/**
	 * 字典类型编辑
	 * 
	 * @param id
	 *            字典类型标识
	 * @param model
	 * @return
	 */
	@RequestMapping(value = { "/admin/configuration/dictionaryTy/edit.htm" })
	public String adminSiteDictionaryTyEdit(final String id) {
		return "admin/configuration/dictionaryTy/edit";
	}
	
	/**
	 * 字典类型分页查询
	 * 
	 * @param dictionaryVo
	 *            字典视图对象
	 * @param model
	 * @return
	 */
	@RequestMapping(value = { "/admin/configuration/dictionaryTy/indexPagination.json" })
	public @ResponseBody
	Map<String, Object> adminSiteDictionaryTyIndexPagination(final DictionaryVo dictionaryVo) {
		final PaginationDto<Map<String, Object>> dictionaryTyPagination = this.commonService
		        .getConfigurationService().getDictionaryService()
		        .paginationDictionaryTyBy1ForMapList(dictionaryVo.getDictionaryTy());
		
		final Map<String, Object> dictionaryTyPaginationMap = Maps.newHashMap();
		
		dictionaryTyPaginationMap.put("total", dictionaryTyPagination.getTotalSize());
		dictionaryTyPaginationMap.put("rows", dictionaryTyPagination.getDataList());
		
		return dictionaryTyPaginationMap;
	}
	
	/**
	 * 新建:字典类型
	 * 
	 * @param dictionaryVo
	 *            字典视图对象
	 * @param model
	 * @return
	 */
	@RequestMapping(value = { "/admin/configuration/dictionaryTy/new.htm" })
	public String adminSiteDictionaryTyNew(final DictionaryVo dictionaryVo) {
		return "admin/configuration/dictionaryTy/new";
	}
	
	/**
	 * 查询:字典类型分页
	 * 
	 * @param dictionaryVo
	 *            字典视图对象
	 * @param model
	 * @return
	 */
	@RequestMapping(value = { "/admin/configuration/dictionaryTy/tree.json" })
	public @ResponseBody
	Map<String, Object> adminSiteDictionaryTyTree(final DictionaryVo dictionaryVo) {
		final Map<String, Object> jsonMap = Maps.newHashMap();
		
		try {
			final List<DictionaryTyDto> dictionaryTyList = this.commonService.getConfigurationService()
			        .getDictionaryService().treeDictionaryTyBy1ForMappedObject(dictionaryVo.getDictionaryTy());
			
			final List<Map<String, Object>> treeMapList = this.dictionaryTyToTreeMapList(dictionaryTyList);
			
			jsonMap.put("dataList", treeMapList);
		} catch (final Exception e) {
			jsonMap.put("message", e.getMessage());
		}
		
		return jsonMap;
	}
	
	/**
	 * 新建:字典类型
	 * 
	 * @param dictionaryVo
	 *            字典视图对象
	 * @param model
	 * @return
	 */
	@RequestMapping(value = { "/admin/configuration/dictionaryTy/validationByCode.json" })
	public @ResponseBody
	Boolean adminSiteDictionaryTyValidationByCode(final DictionaryVo dictionaryVo) {
		try {
			final int count = this.commonService.getConfigurationService().getDictionaryService()
			        .selectDictionaryTyBy201ForObject(dictionaryVo.getDictionaryTy());
			
			return (count < 1);
		} catch (final Exception e) {
		}
		
		return false;
	}
	
	/**
	 * 字典类型查询
	 * 
	 * @param id
	 *            字典类型标识
	 * @param model
	 * @return
	 */
	@RequestMapping(value = { "/admin/configuration/dictionaryTy/view.htm" })
	public String adminSiteDictionaryTyView(final String id) {
		return "admin/configuration/dictionaryTy/view";
	}
	
	private List<Map<String, Object>> dictionaryTyToTreeMapList(final List<DictionaryTyDto> dictionaryTyList) {
		final List<Map<String, Object>> treeMapList = Lists.newArrayList();
		
		if (CollectionUtils.isEmpty(dictionaryTyList)) { return treeMapList; }
		
		for (final DictionaryTyDto dictionaryTy : dictionaryTyList) {
			final Map<String, Object> treeMap = Maps.newHashMap();
			
			treeMap.put("id", dictionaryTy.getId());
			treeMap.put("text", dictionaryTy.getName());
			
			treeMapList.add(treeMap);
			
			if (CollectionUtils.isEmpty(dictionaryTy.getChilds())) {
				continue;
			}
			
			treeMap.put("children", this.dictionaryTyToTreeMapList(dictionaryTy.getChilds()));
		}
		
		return treeMapList;
	}
}
